//
//  CYPassWordInput.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYPassWordInput.h"
@interface CYPassWordInput()<UIKeyInput>{
    PassWordInputComplete _myblock;
}
@property (nonatomic, strong) UIColor *linecolor;
@property (nonatomic, copy) NSMutableString *password; //密码
@property (nonatomic, assign) NSUInteger textNumber; //密码位数
@property (nonatomic, strong) NSMutableArray *cycleArr; //圆圈数组
@end
@implementation CYPassWordInput
- (instancetype)initWithLineColor:(UIColor *)linecolor backgroundColor:(UIColor *)backColor textNumber:(NSUInteger)number size:(CGSize)size complete:(PassWordInputComplete)comlete{
    self = [super initWithColor:backColor];
    if (self) {
        _linecolor = linecolor;
        _textNumber = number;
        _myblock = comlete;
        self.bounds = CGRectMake(0.0, 0.0, size.width, size.height);
        [self setupLineLayer];
    }
    return self;
}

- (NSMutableArray *)cycleArr{
    if (!_cycleArr)
        _cycleArr = [NSMutableArray arrayWithCapacity:_textNumber];
    return _cycleArr;
}

- (NSMutableString *)password{
    if (!_password)
        _password = [NSMutableString stringWithCapacity:_textNumber];
    return _password;
}

- (void)setHidden:(BOOL)hidden{
    [super setHidden:hidden];
    _password = (hidden)?nil:_password;
}

#pragma mark - UIKeyInput
- (BOOL)hasText{
    return YES;
}

- (void)insertText:(NSString *)text{
    
    if (_password.length == _textNumber)
        return;
    
    [self.password appendString:text];
    [self drawCycle:_password.length];//画圆点
    if (_password.length == _textNumber)
        _myblock(_password);//如果密码长度等于预设的长度 则输入完成 ，不执行输入方法
    DeLog(@"text %@ %@",text,_password);
}

- (void)deleteBackward{
    
    if (_password.length <=0) //如果密码长度为0 则不执行删除
        return;
    [_password deleteCharactersInRange:NSMakeRange(_password.length - 1, 1)];
    [self deleteCycle];//删除原点
    DeLog(@"_password %@",_password);
    
}
- (BOOL)canBecomeFirstResponder{
    
    return YES;
}

-(UIKeyboardType)keyboardType{
    return UIKeyboardTypeNumberPad;
}

- (void)setupLineLayer{
    //绘制外框
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *shaplayer = [CAShapeLayer layer];
    [path moveToPoint:CGPointZero];
    [path addLineToPoint:CGPointMake(self.bounds.size.width, 0)];
    [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];
    [path addLineToPoint:CGPointMake(0, self.bounds.size.height)];
    [path addLineToPoint:CGPointZero];
    shaplayer.path = path.CGPath;
    shaplayer.strokeColor = _linecolor.CGColor;
    shaplayer.fillColor = [UIColor clearColor].CGColor;
    shaplayer.lineWidth = 0.2;
    [self.layer addSublayer:shaplayer];
    
    //绘制内框
    CGFloat subWidth = self.bounds.size.width / _textNumber;
    for (int i = 0; i < _textNumber; i ++) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        CAShapeLayer *subLayer = [CAShapeLayer layer];
        [path moveToPoint:CGPointMake(subWidth * (i + 1), 0)];
        [path addLineToPoint:CGPointMake(subWidth * (i + 1), self.bounds.size.height)];
        subLayer.path = path.CGPath;
        subLayer.strokeColor = _linecolor.CGColor;
        subLayer.fillColor = [UIColor clearColor].CGColor;
        subLayer.lineWidth = 0.2;
        [self.layer addSublayer:subLayer];
    }
    
}

//绘制圆圈
- (void)drawCycle:(NSUInteger )index{
    CAShapeLayer *cycleLayer = [CAShapeLayer layer];
    cycleLayer.fillColor = [UIColor blackColor].CGColor;
    CGPoint center = CGPointMake(self.bounds.size.width * 1.0 / _textNumber * (index - 1) + self.bounds.size.width * 1.0 / _textNumber / 2.0, self.bounds.size.height / 2.0);
    cycleLayer.path = [UIBezierPath bezierPathWithArcCenter:center radius:self.bounds.size.height / 10.0 startAngle:0 endAngle:2 * M_PI clockwise:YES].CGPath;
    [self.layer addSublayer:cycleLayer];
    [self.cycleArr addObject:cycleLayer];
    [self setNeedsDisplay];
}

//删除圆圈
- (void)deleteCycle{
    
    [[_cycleArr lastObject] removeFromSuperlayer];
    [_cycleArr removeLastObject];
    [self setNeedsDisplay];
    
}

//清除全部圆圈
- (void)reInput{
    
    for (CAShapeLayer *layer in _cycleArr) {
        [layer removeFromSuperlayer];
    }
    [_cycleArr removeAllObjects];
    _password = nil;
    [self setNeedsDisplay];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
